<?php 
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller{
	//用户登录
	public function login(){ 

		$this->display();
	}
	//用户登录验证
	public function check_login(){
	   if(IS_POST){   
		$username = I("post.username");
		$pwd = I("post.password");
		$code = I("post.yzm");
		$Verify = new \Think\Verify(); 
		if(!$Verify->check($code)){
           $this->error("验证码错误！","",1);
		}
		$userInfo = D("user")->where(array('username'=>$username))->find();
		if(!$userInfo){
           $this->error("用户名错误！","",1);
		  }
		if($userInfo['password'] !== md5($pwd.$userInfo['salt'])){
           $this->error("密码错误！","",1);
		  }else{
		  	cookie("userid",$userInfo['password']);
		  	cookie("username",$userInfo['username']);
		  	$cookie=jm($userInfo['username'].$userInfo['password'].C('COO_KIE'));
		  	cookie('key',$cookie);
		  	$this->success("登录成功！",U("Home/index/index"),2);
		  }
	   }
	}
	// 验证码
	public function verfiy()
	{
		$Verify = new \Think\Verify();
		$Verify->fontSize = 25;
		$Verify->length = 4;
		$Verify->useNoise = false;
		return $Verify->entry();
	}
	//用户退出
	public function loginout()
	{
		cookie('username',null);
		cookie('userid',null);
		cookie('key',null);  // 清空当前设定前缀的所有cookie值
		$this->success("退出成功！",'/',2);
	}
	public function msg()
	{
		$this->display();
	}
	public function reg()
	{
		$this->display();	
	}
	public function regs()
	{
		$userModel = D("User");
		if(IS_POST){
           if(!$userModel->create(I("post."))){
           	   $msg = $userModel->getError();
           	   $this->error($msg,U("Home/user/reg"),2);
           }else{
           	$yan = $this->yan();
           	$userModel->password = md5($userModel->password.$yan);
           	$userModel->salt = $yan;
           	$userModel->add();
           	$this->success("注册成功",U("Home/user/login"),2);
           }
		}

		
	}
	public function yan()
	{
		$str = "mkdawow8377*%4399939u%$^dkasdasska56412";
		return substr(str_shuffle($str),0,8);
	}
}